package com.allwees.core.elasticsearch.query;

import lombok.experimental.Accessors;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;

/**
 *
 *
 * @author yangylsky
 * @since 2020-04-08
 *
 */
@Accessors
public class QueryParams extends HashMap<String, Object> {
	public static QueryParams of() {
		return new QueryParams();
	}

	public String parse(String script) {
		if(CollectionUtils.isEmpty(this)) {
			return script;
		}
		for(String key : this.keySet()) {
			String value = _parseValue(this.get(key));
			script = script.replace("#{" + key + "}", value);
		}
		return script;
	}

	private static String _parseValue(Object obj) {
		if(obj == null) {
			return "";
		}
		if(obj instanceof Date) {
			return String.valueOf(((Date) obj).getTime());
		}
		return obj.toString();
	}

    @Override
	public QueryParams put(String key, Object value) {
    	super.put(key, value);
    	return this;
    }
}
